package com.gdhz.xiaohengqin.web;

import com.gdhz.xiaohengqin.model.dto.UpdateConfigDto;
import com.gdhz.xiaohengqin.model.entity.SysConfig;
import com.gdhz.xiaohengqin.service.SystemConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

/**
 * @author Swallows
 * @date 2025-06-09 15:23
 */
@RestController
@RequestMapping("/api/config")
@RequiredArgsConstructor
public class SystemConfigController {

    private final SystemConfigService systemConfigService;

    @GetMapping("/{paramKey}")
    public Mono<ResponseEntity<String>> getConfig(@PathVariable String paramKey) {
        return systemConfigService.getConfig(paramKey)
                .map(value -> ResponseEntity.ok().body(value))
                .defaultIfEmpty(ResponseEntity.notFound().build());
    }

    @PutMapping("/{paramKey}")
    public Mono<ResponseEntity<SysConfig>> updateConfig(
            @PathVariable String paramKey,
            @RequestBody UpdateConfigDto dto) {
        return systemConfigService.updateConfig(paramKey,  dto.getParamValue())
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.notFound().build());
    }
}

